package api

import (
	"github.com/labstack/echo/v4"
	"longmen/server/pkg/common/api_models"
	"longmen/server/pkg/common/helper/const"
	string2 "longmen/server/pkg/common/helper/string"
	"longmen/server/pkg/common/helper/util"
)

func (base *Controller) GetAccount(c echo.Context) error {
	user, err := base.GetUserByCtx(c)
	if err != nil {
		return util.BuildLoginTimeOut(c, err)
	}
	accounts, err := base.Service.GetAccount(user)
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	return util.BuildSuccess(c, accounts, "")
}

func (base *Controller) EditCashAccount(c echo.Context) error {

	r := &api_models.EditCashAccountReq{}
	if err := util.Json2Struct(c.Get("body").(string), r); err != nil {
		return util.BuildFail(c, 1, "", err)
	}

	if err := _const.Validate.Struct(r); err != nil {
		return util.BadParam(c, err)
	}

	user, err := base.GetUserByCtx(c)
	if err != nil {
		return util.BuildLoginTimeOut(c, err)
	}
	msg, err := base.Service.EditCashAccount(r, user)
	if err != nil {
		return util.BuildFail(c, 1, msg, err)
	}
	return util.BuildSuccess(c, nil, "")
}

func (base *Controller) WithdrawLog(c echo.Context) error {

	r := &api_models.WithdrawlogReq{}
	_ = util.Json2Struct(c.Get("body").(string), r)
	r.Page = string2.GetDefaultNum(r.Page, 1)
	r.Size = string2.GetDefaultNum(r.Size, 20)

	user, err := base.GetUserByCtx(c)
	if err != nil {
		return util.BuildLoginTimeOut(c, err)
	}
	count, err := base.Service.GetAnchorWithdrawalsCount(user.Id)
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	resp, err := base.Service.GetAnchorWithdrawals(user.Id, r.Page, r.Size)
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	return util.BuildSuccess(c, map[string]interface{}{"list": resp, "count": count}, "")
}

// @Summary 獲取銀行卡列表
// @Description 獲取銀行卡列表
// @Accept  json
// @Produce  json
// @Tags Withdraw
// @Success 200 {object} models_main.Banks "result"
// @Router /api/withdraw/banks [POST]
func (base *Controller) Banks(c echo.Context) error {
	resp, err := base.Service.Banks()
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	return util.BuildSuccess(c, map[string]interface{}{"list": resp, "count": len(resp)}, "")
}

// @Summary 提现密码验证
// @Description 提现密码验证
// @Accept  json
// @Produce  json
// @Tags Withdraw
// @Param   sendBody body api_models.UserWithdrawPass true "body"
// @Success 200 {object} api_models.Response "result"
// @Router /api/withdraw/verifyWithdrawPass [POST]
func (base *Controller) VerifyWithdrawPass(c echo.Context) error {
	r := &api_models.UserWithdrawPass{}
	_ = util.Json2Struct(c.Get("body").(string), r)
	if err := _const.Validate.Struct(r); err != nil {
		return util.BadParam(c, err)
	}
	user, err := base.GetUserByCtx(c)
	if err != nil {
		return util.BuildLoginTimeOut(c, err)
	}
	if user.PayPassword == "" {
		//return common.BuildFail(c, 2, "未设置取款密码", err)
	}
	e := base.Service.VerifyWithdrawPass(r, user)
	if e != nil {
		return util.BuildFail(c, 1, "", e)
	}
	return util.BuildSuccess(c, nil, "")
}

// @Summary 提交提現表單
// @Description 提交提現表單
// @Accept  json
// @Produce  json
// @Tags Withdraw
// @Param   sendBody body api_models.UserWithdrawReq true "body"
// @Success 200 {object} api_models.Response "result"
// @Router /api/withdraw/add [POST]
func (base *Controller) AddWithdraw(c echo.Context) error {
	r := &api_models.UserWithdrawReq{}
	_ = util.Json2Struct(c.Get("body").(string), r)
	if err := _const.Validate.Struct(r); err != nil {
		return util.BadParam(c, err)
	}
	user, err := base.GetUserByCtx(c)
	if err != nil {
		return util.BuildLoginTimeOut(c, err)
	}
	e := base.Service.AddWithdraw(r, user)
	if e != nil {
		return util.BuildFail(c, 1, "", e)
	}
	return util.BuildSuccess(c, nil, "")
}

// @Summary 獲取提現記錄列表
// @Description  獲取提現記錄列表
// @Accept  json
// @Produce  json
// @Tags Withdraw
// @Param   sendBody body api_models.WithdrawsReq true "body"
// @Success 200 {object} api_models.WithdrawsRsp "result"
// @Router /api/withdraw/withdraws [POST]
func (base *Controller) Withdraws(c echo.Context) error {
	r := &api_models.WithdrawsReq{}
	_ = util.Json2Struct(c.Get("body").(string), r)
	if err := _const.Validate.Struct(r); err != nil {
		return util.BadParam(c, err)
	}

	user, err := base.GetUserByCtx(c)
	if err != nil {
		return util.BuildLoginTimeOut(c, err)
	}
	resp, err := base.Service.Withdraws(r, user.Id)
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	return util.BuildSuccess(c, map[string]interface{}{"list": resp, "count": len(resp)}, "")
}
